﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Abp.Application.Services.Dto;
using Abp.AspNetCore.Mvc.Authorization;
using Abp.UI;
using Microsoft.AspNetCore.Mvc;
using Partner.Surround.Controllers;
using Partner.Surround.Core.DataDictionaries;
using Partner.Surround.Core.DataDictionaries.DataDictionaryItems;
using Partner.Surround.Core.DataDictionaries.DataDictionaryItems.Dto;
using Partner.Surround.Core.DataDictionaries.Dto;
using Partner.Surround.Web.Models.Common;
using Partner.Surround.Web.Mvc.Areas.Core.Models.DataDictionaries;

namespace Partner.Surround.Web.Mvc.Areas.Core.Controllers
{
    /// <summary>
    /// 数据字典控制器
    /// </summary>
    [Area("Core")]
    [AbpMvcAuthorize]
    public class DataDictionaryController : SurroundControllerBase
    {
        #region 初始化
        private readonly IDataDictionaryAppService _dataDictionaryAppService;
        private readonly IDataDictionaryItemAppService _dataDictionaryItemAppService;

        public DataDictionaryController(IDataDictionaryAppService dataDictionaryAppService,
            IDataDictionaryItemAppService dataDictionaryItemAppService)
        {
            _dataDictionaryAppService = dataDictionaryAppService;
            _dataDictionaryItemAppService = dataDictionaryItemAppService;
        }
        #endregion

        #region 数据字典
        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取数据字典列表
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> GetAllDataDictionaryList()
        {
            var dataDictionaryListDto = (await _dataDictionaryAppService.GetAllDataDictionaryListAsync()).Items;
            return Json(dataDictionaryListDto);
        }

        /// <summary>
        /// 创建或更新数据字典
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        public async Task<IActionResult> CreateOrUpdateDataDictionary(DataDictionaryRequestViewModel viewModel)
        {
            var dataDictionaryEditDto = await _dataDictionaryAppService.GetDataDictionaryForEditAsync(new NullableIdDto<long>(viewModel.DataDictionaryId));
            return View(dataDictionaryEditDto);
        }

        /// <summary>
        /// 创建或更新数据字典
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> CreateOrUpdateDataDictionary([FromBody]CreateOrUpdateDataDictionaryViewModel viewModel)
        {
            await _dataDictionaryAppService.CreateOrUpdateDataDictionaryAsync(new CreateOrUpdateDataDictionaryInput()
            {
                DataDictionary = viewModel.DataDictionary
            });

            return Json(new ResponseParamViewModel(viewModel.DataDictionary.Id.HasValue ? L("更新字典类型成功") : L("新增字典类型成功")));
        }

        /// <summary>
        /// 删除数据字典
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> DeleteDataDictionary([FromBody]List<EntityViewModel<long>> ids)
        {
            var input = ObjectMapper.Map<List<EntityDto<long>>>(ids);
            await _dataDictionaryAppService.DeleteDataDictionaryAsync(input);

            return Json(new ResponseParamViewModel(L("删除字典类型成功")));
        }
        #endregion

        #region 数据字典项
        /// <summary>
        /// 获取数据字典项列表
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        public async Task<JsonResult> GetDataDictionaryItemList(DataDictionaryItemRequestViewModel viewModel)
        {
            var dataDictionaryItemListDto = await _dataDictionaryItemAppService.GetDataDictionaryItemListAsync(new GetDataDictionaryItemListInput()
            {
                DataDictionaryId = viewModel.DataDictionaryId.Value
            });

            return Json(new ListResultViewModel<DataDictionaryItemListDto>(dataDictionaryItemListDto.Items));
        }

        /// <summary>
        /// 创建或更新数据字典项
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        public async Task<IActionResult> CreateOrUpdateDataDictionaryItem(DataDictionaryItemRequestViewModel viewModel)
        {
            if (!viewModel.DataDictionaryId.HasValue && !viewModel.DataDictionaryItemId.HasValue)
            {
                throw new UserFriendlyException(L("请求参数有误"));
            }

            var dataDictionaryItemEditDto = await _dataDictionaryItemAppService.GetDataDictionaryItemForEditAsync(new NullableIdDto<long>(viewModel.DataDictionaryItemId));

            if (!viewModel.DataDictionaryItemId.HasValue)
            {
                dataDictionaryItemEditDto.DataDictionaryId = viewModel.DataDictionaryId.Value;
            }

            return View(dataDictionaryItemEditDto);
        }

        /// <summary>
        /// 创建或更新数据字典项
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> CreateOrUpdateDataDictionaryItem([FromBody]CreateOrUpdateDataDictionaryItemViewModel viewModel)
        {
            var input = ObjectMapper.Map<CreateOrUpdateDataDictionaryItemInput>(viewModel);
            await _dataDictionaryItemAppService.CreateOrUpdateDataDictionaryItemAsync(input);

            return Json(new ResponseParamViewModel(viewModel.DataDictionaryItem.Id.HasValue ? L("更新字典项成功") : L("新增字典项成功")));
        }

        /// <summary>
        /// 删除数据字典项
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> DeleteDataDictionaryItem([FromBody]List<EntityViewModel<long>> ids)
        {
            var input = ObjectMapper.Map<List<EntityDto<long>>>(ids);
            await _dataDictionaryItemAppService.DeleteDataDictionaryItemAsync(input);

            return Json(new ResponseParamViewModel(L("删除字典项成功")));
        }

        /// <summary>
        /// 根据字典类型获取字典项
        /// </summary>
        /// <param name="type">字典类型</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> GetDataDictionaryItem([FromBody]List<string> typeNames)
        {
            var dataDictionaryItemList = await _dataDictionaryAppService.GetDataDictionaryListByTypeNamesAsync(new GetDataDictionaryListByTypeNamesInput()
            {
                TypeNames = typeNames
            });

            return Json(new ListResultViewModel<DataDictionaryListDto>(dataDictionaryItemList.Items));
        }
        #endregion
    }
}